package cn.stylefeng.guns.base.utils;

import cn.hutool.core.date.SystemClock;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * @program: football_manage
 * @description: 钉钉
 * @author: xiesm
 * @create: 2023-09-15 09:44
 **/
public class DingUtil {

    private static List<String> secrets = Arrays.asList(
            "",  //重复2
            "",  //连续5-6
            "",  //号码=漏
            "",  //连3
            "",  //连4
            "",  //连6
            "",  //差10
            "",  //连续差异
            "",   //号
            ""   //相同
    );

    private static List<String> urls = Arrays.asList(
            "https://oapi.dingtalk.com/robot/send?access_token=7c293a84a87768f4a1f805fb4ca159fbae079ec39229fad7d67f5de66f0c800e",
            "https://oapi.dingtalk.com/robot/send?access_token=2b815822bf3a5d7a30bf1c3dddceadce9c651d8e8b332c6a709d20bcab021d13",
            "https://oapi.dingtalk.com/robot/send?access_token=60088f60fd1faf50087972ee65ea126d936f16ae948f11a915397ba83a50ce2e",
            "https://oapi.dingtalk.com/robot/send?access_token=7f9333441774a1cbb6ee1fd68efbc21ae10f54748af5ed9439159403c2031e73",
            "https://oapi.dingtalk.com/robot/send?access_token=7545cf7b3afaa70d64c2d07d98ceb373fd1219457fc14c79ee8315f8a4241c27",
            "https://oapi.dingtalk.com/robot/send?access_token=b4b9d59bcf05055192bde047efd29c2000046d21bedbd2bbcf1d930a13359041",
            "https://oapi.dingtalk.com/robot/send?access_token=317b5d09534da8921338211ec1e5da3d6d3c60d75a5754809950634dcecc5ba9",
            "https://oapi.dingtalk.com/robot/send?access_token=a7914f052d98541f47838bcfff8dee23758bd96b50ef36d0f453a4d80fddb1bc",
            "https://oapi.dingtalk.com/robot/send?access_token=e8811d6c649505e348f7d90555bf3b428e0d83ad0b06e8ed7b1e2f1a18b44bc4",
            "https://oapi.dingtalk.com/robot/send?access_token=f69bc8fac57a9de1b4d16fb47136144e3954cbc3283b831682fba701776ea860"
    );

    @SneakyThrows
    public static String getSign(Long timestamp, String flag, String secret) {
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
        return sign;
    }

    public static String getUrl(Long timestamp, String flag) {
        String secret = secrets.get(Integer.valueOf(flag) - 1);
        String url = urls.get(Integer.valueOf(flag) - 1);
        url = StrUtil.format("{}&sign={}&timestamp={}", url);
        String sign = getSign(timestamp, flag, secret);
        String path = StrUtil.format(url, sign, timestamp);
        return path;
    }

    public static void send(String content, String flag) {
        JSONObject params = new JSONObject();
        params.put("msgtype", "text");

        JSONObject data = new JSONObject();
        data.put("content", content);
        params.put("text", data);
        Long timestamp = SystemClock.now();

        HttpUtil.post(getUrl(timestamp, flag), params.toJSONString());
    }

    public static void main(String[] args) {
//        send("11111111111", "1");
//        send("22222222222", "4");
//        System.out.println(StrUtil.padPre("1", 2, "0"));

//        StringBuilder sb = new StringBuilder();
//        sb.append("1111").append("|").append("2222").append("|").append("3333").append("|");
//        String s = StrUtil.subBefore(sb.toString(), "|", false);
//        System.out.println(s);
        //https://h5.jntybet.com/#/login
        String url = "https://www.jntybet.com/api/member/captcha/create?codeNonce=17018255341647296&ts=1701825534&nonce=05dd58c00ea92eddbe52&sign=3f71494c674e5c6da8032254bf2a0c27";
        String s = HttpUtil.createGet(url)
//                .header("X-Auth-Token", "a07ae156-c7c9-4d49-87c9-a8238e2bcc4e")
                .execute().body();
        System.out.println(s);

    }
}
